package com.gome.boot.config.tddl;

import com.gome.boot.config.datasource.single.BaseMybatisConfig;
import com.taobao.tddl.matrix.jdbc.TDataSource;

import javax.sql.DataSource;

/**
 * TDDL数据源基础配置.
 *
 * @author baoxiufeng
 */
public abstract class BaseTddlConfig extends BaseMybatisConfig {

    private static final String KEY_TDDL_APP_NAME= "tddl.appName";

    @Override
    protected DataSource getDataSource() throws Exception {
        return getDataSource(null);
    }

    @Override
    protected DataSource getDataSource(String type) throws Exception {
        TDataSource dataSource = new TDataSource();
        if (type == null) {
            dataSource.setAppName(env.getRequiredProperty(KEY_TDDL_APP_NAME));
        } else {
            dataSource.setAppName(env.getRequiredProperty(type + "." + KEY_TDDL_APP_NAME));
        }
        dataSource.setDynamicRule(Boolean.TRUE);
        return dataSource;
    }
}
